
import routes from '../router/index'
// const getBreadcrumbData = (pathname) => {
//     console.log(pathname, '获取动态路由信息')
//     const breadcrumbData = [];
//     const pathSegments = pathname.split('/').filter(Boolean);

//     let currentPath = '';
//     pathSegments.forEach((segment) => {
//         currentPath += `/${segment}`;
//         const meta = getCurrentRouteMeta(currentPath);
//         breadcrumbData.push({
//             path: currentPath,
//             label: meta ? meta.title : segment.charAt(0).toUpperCase() + segment.slice(1)

//         });
//     });

//     return breadcrumbData;
// };
// const getCurrentRouteMeta = (pathname) => {
//     const matchRoute = routes.find(route => route.path === pathname);
//     if (matchRoute) {
//       return matchRoute.meta;
//     } else {
//       const segments = pathname.split('/').filter(Boolean);
//       for (let i = 0; i < segments.length; i++) {
//         const partialPath = `/${segments.slice(0, i + 1).join('/')}`;
//         const match = routes.find(route => route.path === partialPath);
//         if (match && match.children) {
//           const childMatch = match.children.find(childRoute => childRoute.path === `/${segments.slice(i + 1).join('/')}`);
//           if (childMatch) {
//             return childMatch.meta;
//           }
//         }
//       }
//     }
//     return null;
//   };
// export default getBreadcrumbData


// const getCurrentRouteMeta = (pathname) => {
//     const matchRoute = routes.find(route => route.path === pathname);
//     if (matchRoute) {
//       return matchRoute.meta;
//     } else {
//       const segments = pathname.split('/').filter(Boolean);
//       for (let i = 0; i < segments.length; i++) {
//         const partialPath = `/${segments.slice(0, i + 1).join('/')}`;
//         const match = routes.find(route => route.path === partialPath);
//         if (match && match.children) {
//             console.log(match.children,'获取路由',`/${segments.slice(i + 1).join('/')}`)
//           const childMatch = match.children.find(childRoute => childRoute.path === `/layout/${segments.slice(i + 1).join('/')}`);
//           if (childMatch) {
//             return childMatch.meta;
//           }
//         }
//       }
//     }
//     return null;
//   };

//   export default getCurrentRouteMeta

const getCurrentRouteMeta = (pathname) => {
  const findRouteMeta = (routes) => {
    for (const route of routes) {
      if (route.path === pathname) {
        return route.meta;
      } else if (route.children) {
        const childMeta = findRouteMeta(route.children);
        if (childMeta) {
          return childMeta;
        }
      }
    }
    return null;
  };

  return findRouteMeta(routes);
};

export default getCurrentRouteMeta